
define([
	'vue',
	'config',
	'common',
	'../model/upLoadList',
	'../model/downLoadList',
	'moment'
	],function(Vue,config,utils,upLoadList,downLoadList,moment){
	'use strict';
	
	var consumeRecordView=new Vue({
		el:'#app',
		data:{
			items:'',
			page: 1,
			totalPage:0,
			isLoadMore:0,
			pageSize:''
		},
		filters:{
			moment:function(date){
				var Date = moment(date*1000).format('YYYY-MM-DD');
				var arr = Date.split('-');
				return arr[1]+'-'+arr[2];
			}
		},
		watch:{
			items: function(to,from){
				if(to.length == 0 && this.items !== ""){
					$('.error404').show();
				}else{
					$('.error404').hide();
				}
			}
		},
		created:function(){
			utils.userToken();

			var _this=this;
			_this.pageSize = parseInt(Math.ceil(($('body').height()-$('.headTitle').height()-$('.title').height())/44), 10);
			upLoadList.ajax(utils.getWebUrl('declaration/ulist'),{
				pageSize: _this.pageSize,
				page: _this.page
			},function(res){
				_this.items = res.data.list;
				_this.totalPage = res.data.totalPage;
			})
		},
		directives: {
		  scroll: {
		    bind: function (el, binding){
		      window.addEventListener('scroll',function() {
		       		var fnc = binding.value;   
   					fnc();
		      })
		    }
		  }
		},
		methods:{

			loadMore:function(event){

				var _this = this;
				//console.log(upLoadList.getScrollTop() + upLoadList.getClientHeight(),upLoadList.getScrollHeight(),_this.page)
				if ((upLoadList.getScrollTop() + upLoadList.getClientHeight() >= upLoadList.getScrollHeight())&&_this.page<_this.totalPage) {
					_this.page = _this.page+1;
					var data = {
		           		pageSize: _this.pageSize,
						page: _this.page
		           	}
		           	
		            upLoadList.ajax(utils.getWebUrl('declaration/ulist'),data,function(res){
						 _this.items = _this.items.concat(res.data.list);
					})
		        }

			},
			
			Start:function(event){

				var touch = event.targetTouches[0];
				this.start = touch.pageY;

			},

			Move:function(event){
				var touch = event.targetTouches[0];
				this.end = (this.start - touch.pageY);
				var slideDown1 = $('#pullDown1');
				//下滑才执行操作 
                if(this.end < 0){
                	
                	slideDown1.text('下拉刷新  ↓');
	        		slideDown1.show();
	        		slideDown1[0].style.height =  (parseInt("0px")-this.end) + "px";
	        		if(this.end < -48){
	        			/*slideDown1.hide(); 
	        			downLoadList.slideDownStep1(this.end);*/
	        			slideDown1.text('松手开始更新  ↑')
	        		}
                }else{
                	slideDown1.hide();
                }
			},

			End:function(event){
				var _this = this;
				var slideDown1 = $('#pullDown1');
				if(this.end >0){  
					slideDown1.hide()
	            }else{ 

	                if(this.end < -48 && document.body.scrollTop == 0){
	                	var slideDown1 = $('#pullDown1');
	                	var slideDown2 = $('#pullDown2');
						//$('.pullDownLabel').text("下拉刷新...");
						slideDown1.hide();
				        slideDown2.show();

	                	downLoadList.slideDownStep2(); 
		                //刷新成功则 
		                //模拟刷新成功进入第三步
		                _this.items = '';
		                _this.page = 1;
		                var data = {
							page: _this.page,
							pageSize: _this.pageSize
						}
						$('#pullDown3').hide();
		                utils.ajax(utils.getWebUrl('declaration/ulist'),data,
		                	'POST',
		                	function(res){
								_this.items = res.data.list;
								_this.totalPage = res.data.totalPage;
						},null,function(){
							downLoadList.slideDownStep3();
						})
		                
	                }else{
	                	var slideDown1 = $('#pullDown1');
		        		slideDown1.hide();
	                }
	            }
			},


			toDetail:function(event){
				var id = $(event.currentTarget).data('id');
				window.location.href="consumerInquiriesDetail_user.html?id="+id;
			}
		}
	})

	return consumeRecordView;
});